public class TimeZoneTest {

    @Test
    public void getLocalTimeZone() {
        Calendar cal = Calendar.getInstance();
        TimeZone timeZone = cal.getTimeZone();
        System.out.println(timeZone.getID());

        TimeZone timeZone1 = TimeZone.getDefault();
        System.out.println(timeZone1.getID());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        TimeZone timeZone2 = simpleDateFormat.getTimeZone();
        System.out.println(timeZone2.getID());
    }

    @Test
    public void setTimeZone() {
        TimeZone timeZone = TimeZone.getTimeZone("GMT+2");
        System.out.println(timeZone.getID());

        TimeZone timeZone1 = TimeZone.getTimeZone("Asia/Shanghai");
        System.out.println(timeZone1.getID());
    }

    @Test
    public void setTimeZoneToTime() {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2"));
        cal.setTime(new Date());
        System.out.println((cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+2"));
        System.out.println(simpleDateFormat.format(new Date()));

        System.out.println(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT+2")));
    }

    @Test
    public void getTimeZoneOffset() {
        Calendar cal = Calendar.getInstance();
        long offset = cal.get(Calendar.ZONE_OFFSET);
        System.out.println(offset/60/60/1000);

        TimeZone timeZone = TimeZone.getDefault();
        long offset1 = timeZone.getOffset(Calendar.ZONE_OFFSET);
        System.out.println(offset1/60/60/1000);
    }

    @Test
    public void getTimeZoneDayStartAndEndTest() {
        getTimeZoneDayStartAndEndByZone(TimeZone.getTimeZone("GMT+2"), new Date());
        getTimeZoneDayStartAndEndByOffset(TimeZone.getTimeZone("GMT+2"), new Date());
    }

    public void getTimeZoneDayStartAndEndByZone(TimeZone timeZone, Date clientTime) {
        Calendar cal = Calendar.getInstance(timeZone);
        cal.setTime(clientTime);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date clientDayStartTime = cal.getTime();
        cal.add(Calendar.HOUR_OF_DAY, 24);
        Date clientDayEndTime = cal.getTime();
        System.out.println(DateFormatUtils.format(clientDayStartTime,"yyyy-MM-dd HH:mm:ss") +
                " -- " + DateFormatUtils.format(clientDayEndTime,"yyyy-MM-dd HH:mm:ss"));
    }

    public void getTimeZoneDayStartAndEndByOffset(TimeZone timeZone, Date clientTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(clientTime);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long offset = TimeZone.getDefault().getOffset(Calendar.ZONE_OFFSET) - timeZone.getOffset(Calendar.ZONE_OFFSET);
        cal.add(Calendar.MILLISECOND,(int)offset);
        Date clientDayStartTime = cal.getTime();
        cal.add(Calendar.HOUR_OF_DAY, 24);
        Date clientDayEndTime = cal.getTime();
        System.out.println(DateFormatUtils.format(clientDayStartTime,"yyyy-MM-dd HH:mm:ss") +
                " -- " + DateFormatUtils.format(clientDayEndTime,"yyyy-MM-dd HH:mm:ss"));
    }
}